<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 照片管理
 */
class PhotoController extends BaseController {

    /**
     * 照片列表
     */
    public function photoList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        $photo = M('Photo');
        //数据总数
        $total = $photo->count();

        //数据列表
        $list = $photo->field('p.*,u.name user_name,u.phone')
                ->alias('p')
                ->join('LEFT JOIN __USERS__ u ON p.user_id=u.id')
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['add_time'] = !empty($item['add_time']) ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
            }
        }

        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('orderField', $orderField);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('total', $total);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 删除照片
     */
    public function delPhoto() {
        $ids = I('get.ids', '');
        if (empty($ids)) {
            ajaxReturn('请选择删除项', 300);
        }
        $photo = M('Photo');
        $ret = $photo->where("FIND_IN_SET(id,'{$ids}')")->delete();
        if ($ret) {
            ajaxReturn('删除成功', 200, '', '', '', 'Admin/Photo/photoList');
        } else {
            ajaxReturn('删除失败', 300);
        }
    }

}
